//
// Copyright (C) 2024 Kumo group inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <krpc/var/config.h>
#include <turbo/strings/match.h>

TURBO_FLAG(int32_t, kvar_latency_p2, 90, "Second latency percentile").on_validate(
        [](std::string_view value, std::string *err) noexcept -> bool {
            int32_t val;
            if (!turbo::parse_flag(value, &val, err)) {
                return false;
            }
            if (val <= 0 || val >= 100) {
                if (err)
                    *err = "Percentile must be in (0, 100)";
                return false;
            }
            if (val >= turbo::get_flag(FLAGS_kvar_latency_p3)) {
                if (err)
                    *err = "Percentile must be less than var_latency_p3";
                return false;
            }
            if (val <= turbo::get_flag(FLAGS_kvar_latency_p1)) {
                if (err)
                    *err = "Percentile must be less than var_latency_p3";
                return false;
            }
            return true;
        });
TURBO_FLAG(int32_t, kvar_latency_p1, 80, "First latency percentile").on_validate(
        [](std::string_view value, std::string *err) noexcept -> bool {
            int32_t val;
            if (!turbo::parse_flag(value, &val, err)) {
                return false;
            }
            if (val <= 0 || val >= 100) {
                if (err)
                    *err = "Percentile must be in (0, 100)";
                return false;
            }
            if (val >= turbo::get_flag(FLAGS_kvar_latency_p2)) {
                if (err)
                    *err = "Percentile must be less than var_latency_p2";
                return false;
            }
            return true;
        });
TURBO_FLAG(int32_t, kvar_latency_p3, 99, "Third latency percentile").on_validate(
        [](std::string_view value, std::string *err) noexcept -> bool {
            int32_t val;
            if (!turbo::parse_flag(value, &val, err)) {
                return false;
            }
            if (val <= 0 || val >= 100) {
                if (err)
                    *err = "Percentile must be in (0, 100)";
                return false;
            }
            if (val <= turbo::get_flag(FLAGS_kvar_latency_p2)) {
                if (err)
                    *err = "Percentile must be larger than var_latency_p2";
                return false;
            }
            return true;
        });

TURBO_FLAG(uint64_t, latency_scale_factor, 1, "latency scale factor, used by method status, etc., latency_us = latency * latency_scale_factor");


// TODO: Do we need to expose this flag? Dumping thread may dump different
// kind of samples, users are unlikely to make good decisions on this value.
TURBO_FLAG(int32_t, kvar_collector_max_pending_samples, 1000,
           "Destroy unprocessed samples when they're too many");

TURBO_FLAG(int32_t, kvar_collector_expected_per_second, 1000,
           "Expected number of samples to be collected per second");

TURBO_FLAG(int32_t, kvar_sampler_thread_start_delay_us, 10000, "kvar sampler thread start delay us");
TURBO_FLAG(bool, kvar_enable_sampling, true, "is enable kvar sampling");

TURBO_FLAG(int32_t, kvar_max_multi_dimension_metric_number, 1024, "Max number of multi dimension").on_validate(turbo::GeValidator<int32_t, 1>::validate);
TURBO_FLAG(int32_t, kvar_max_dump_multi_dimension_metric_number, 1024,
           "Max number of multi dimension metric number to dump by prometheus rpc service").on_validate(turbo::GeValidator<int32_t, 1>::validate);

TURBO_FLAG(bool, quote_vector, true,
           "Quote description of Vector<> to make it valid to noah");